<?php

namespace App\Http\Services\Manage;

use App\Models\LouAddress;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class ImportDataService implements ToCollection
{
    protected $type;

    /**
     * @param $type 1: 就诊人员 / 其他 2 职工
     */
    public function __construct($type)
    {
        $this->type = intval($type);
    }

    public function collection(Collection $rows)
    {
        $this->formatData($rows);
    }

    private function formatData($rows)
    {
        unset($rows[0]);
        $s = [];
        $list = [];
        $continueLine = 0;
        foreach ($rows as $line => $row) {
            if (is_null($row[1]) && is_null($row[2]) && is_null($row[3])) {
                $continueLine = $line + 1;
                continue;
            }

            if ($continueLine == $line) {
                $continueLine = 0;
                continue;
            }
            // A B C D E F G H I J K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU
            // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

            // O 上次家访谁和孩子一起活动 1妈妈 2看护人和其他成年人 3其他孩子

            // P O的说明 O为2/3时 注明孩子和谁一起活动

            // Q 上次家访之后，有多常与孩子一起玩要”，1为“很少《0-2天/周》或没有”，2为 “只有几天（3一4天/周》”，3为“几乎每天（5-7天/周），直接填写数字即可；

            // 活动 1单独完成 2需要帮助 3完全不能

            // AD 备注和说明

            // AT 备注和说明

            // AU 其他说明
            if (empty($row->toArray()))
                continue;

            $list[] = $row->toArray();
        }

        $data = [];
        foreach ($list as $k => &$v) {
            $v[0] = str_replace("号楼", "", $v[0]);
            $v[1] = str_replace("层", "", $v[1]);
            $v[1] = str_replace("F", "", $v[1]);
            $v[3] = str_replace("床", "", $v[3]);
            $data[$v[0]][$v[1]][$v[2]][] = $v[3];
        }

        if (count($data) > 0) {
            $data1 = array_unique(array_keys($data));
            $insertData = $this->_format_data($data1);
            LouAddress::insert($insertData);

            $insertData2 = [];
            foreach ($data as $k => $level2) { //2层
                $pid = LouAddress::query()->where(["title" => $k, 'level' => 1])->value("id");
                if (!$pid) continue;

                $data2 = array_unique(array_keys($level2));
                $insertData_tmp = $this->_format_data($data2, $pid, 2);
                $insertData2 = array_merge($insertData_tmp, $insertData2);
            }
            LouAddress::insert($insertData2);

            //3层
            $insertData3 = [];
            foreach ($data as $k => $v) { //3层

                $pid1 = LouAddress::query()->where(["title" => $k, 'level' => 1])->value("id");
                foreach ($v as $k1 => $v1) {
                    if (empty($v1)) continue;
                    $pid = LouAddress::query()->where(["title" => $k1, 'level' => 2, 'pid' => $pid1])->value("id");
                    if (!$pid) continue;

                    $data3 = array_unique(array_keys($v1));
                    $insertData_tmp = $this->_format_data($data3, $pid, 3);
                    $insertData3 = array_merge($insertData_tmp, $insertData3);
                }
            }
            LouAddress::insert($insertData3);

            //4层
            if ($this->type == 1) {
                $insertData4 = [];
                foreach ($data as $k => $v) { //3层
                    foreach ($v as $k1 => $v1) {
                        if (empty($v1)) continue;

                        $pid2 = LouAddress::query()->where(["title" => $k, 'level' => 2, 'pid' => $k1])->value("id");
                        foreach ($v1 as $k2 => $v2) {
                            if (count($v2) == 0) continue;

                            $pid = LouAddress::query()->where(["title" => $k2, 'level' => 3, 'pid' => $pid2])->value("id");
                            if (!$pid) continue;

                            $insertData_tmp = $this->_format_data($v2, $pid, 4);
                            $insertData4 = array_merge($insertData_tmp, $insertData4);
                        }
                    }
                }

                LouAddress::insert($insertData4);
            }
        }
    }

    private function _format_data($data = null, $pid = 0, $level = 1)
    {
        if (empty($data))
            return null;

        $louAddressList = LouAddress::query()->where(['level' => $level, 'pid' => $pid, 'type' => $this->type])->pluck("title")->toArray();

        $insertData = [];
        foreach ($data as $k1 => $title) {
            if (empty($title) || in_array($title, $louAddressList)) continue;

            $insertData[] = [
                'pid' => $pid,
                'level' => $level,
                'sort' => (is_numeric($title) ? (intval($title) * 10) : 80),
                'type' => intval($this->type ?? 0),
                'title' => $title,
                'create_time' => date("Y-m-d H:i:s"),
                'update_time' => date("Y-m-d H:i:s"),
            ];
        }

        return $insertData;
    }

}